<?php
/**
 * 离职列表
 */
apf_require_controller("Abstract");
apf_require_class("Bll_Report_Report");
apf_require_class("Report_Urls");
class Report_DimissionViewController extends AbstractController {
    private $report_bll;
    public function handle_request() {
        $params = $this->request->get_parameters();
        $this->report_bll = new Bll_Report_Report();
        $quart=$this->_current_quarter();
        $params['fid'] = empty($params['fid']) ? 0 : intval($params['fid']);
        $params['start'] = empty($params['start']) ? $quart[0] : $params['start'];
        $params['end'] = empty($params['end']) ? date('Y-m-d') : $params['end'];
        $params['type'] = empty($params['type']) ? 'season' : $params['type'];

        $params['start'] = date('Ymd',strtotime($params['start']));
        $params['end'] = date('Ymd',strtotime($params['end']));

        $this->fun_list = Bll_Hr_Property::get_function_list();
        $bu_list = Bll_Hr_Property::get_business_list();
        $data['datalist'] = $this->_get_list($params);
        //allow department_id
        $allow_depids = $this->report_bll->get_role_dep_ids($this->user_info['user_id']);

        $title = empty($params['fid']) ? '' : $this->fun_list[$params['fid']]['function_name'];
        $this->request->set_attribute('allow_depids',$allow_depids);
        $this->request->set_attribute('title',$title);
        $this->request->set_attribute('params', $params);
        $this->request->set_attribute('fun_list', $this->fun_list);
        $this->request->set_attribute('bu_list', $bu_list);
        $this->request->set_attribute('data', $data);

        return "Report_DimissionView";
    }
    /**
     * 1.(职能/事业部/城市)入职0离职0
     * 2.(职能/事业部/城市)入职1离职0
     * 对数据进行补充
     * @param unknown_type $params
     */
    private function _get_list($params){
        $_where = array();
        if(!empty($params['fid'])){
            $_where['function_id'] = $params['fid'];
            //根据职能取所有的事业部
            $hroa_bll = new Bll_Hr_OutInterface();
            $filldata=$hroa_bll->get_bu_by_function($params['fid']);
        }else{
            $filldata = $this->fun_list;
        }
        if(!empty($params['start']) && !empty($params['end'])){
            $_where['start'] = $params['start'];
            $_where['end'] = $params['end'];
        }
        //数据模板
        $tpl = $this->report_bll->build_data_tpl();
        $data = $this->report_bll->search_dimission_list($_where);
        $datalist[999] = $data[999];
        foreach($filldata as $fid=>$val){
            if(empty($data[$fid])){
                $datalist[$fid] =$tpl;
            }else{
                $datalist[$fid] =$data[$fid];
            }
        }
        return $datalist;
    }

    /**
     * 当前季度的第一天
     * @return string
     */
    private function _current_quarter(){
        $month = date('n');
        $s = $month/3;
        $m = $s<1 ? 1 : ceil($s);
        $mm = ($m-1)*3+1;

        $start= date('Y-m-d',mktime(0,0,0,$mm,1,date('Y')));
        $end = date('Y-m-d',mktime(0,0,0,$mm+2,0,date('Y')));
        return array($start,$end);
    }
}